export default {
  model: {
    prop: 'show',
    event: 'showChange'
  },
  props: {
    // 是否显示模态框
    show: { type: Boolean, default: false }
  },
  data () {
    return {
      modal: {
        // 主键
        primary: 'id',
        // formData
        data: {},
        // 是否显示模态框
        show: false,
        // 提交加载状态
        load: false,
        // 新增标题
        addTitle: '新建',
        // 修改标题
        editTitle: '修改',
        // 添加操作时的URL
        addUrl: undefined,
        // 修改操作时的URL
        editUrl: undefined,
        // 自定义提交函数
        customSubmit: undefined
      }
    }
  },
  computed: {
    title () {
      return this.modal.data[this.modal.primary]
        ? this.modal.editTitle
        : this.modal.addTitle
    }
  },
  methods: {
    // add参数转换钩子函数
    async addTransform () {
      return {}
    },
    // edit参数转换钩子函数
    async editTransform (data) {
      return data
    },
    openAfter () {},
    // 提交时参数转换钩子函数
    submitTransform (data) {
      return data
    },
    // 打开模态框
    async open (params, type) {
      this.$emit('showChange', true)
      this.modal.show = true
      this.modal.load = true
      try {
        if (params && params[this.modal.primary]) {
          this.modal.data = await this.editTransform({ ...params }, type)
        } else {
          this.modal.data = await this.addTransform()
        }
        this.openAfter()
      } catch (e) {
        this.$message.newFun('获取数据出错', 'error', 3)
        this.close()
      } finally {
        this.modal.load = false
      }
    },
    // 提交前验证数据
    handleSave () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.axiosSubmit()
        }
      })
    },
    // 提交
    axiosSubmit () {
      const formData = { ...this.modal.data }
      if (formData[this.modal.primary]) { // 修改
        this.Submit(this.modal.editUrl, '未设置editUrl', formData)
          .then(this.SubmitAfter).catch(() => {
            this.$message.newFun('提交出错', 'error', 3)
            this.modal.load = false
          })
      } else { // 新建
        this.Submit(this.modal.addUrl, '未设置addUrl', formData)
          .then(this.SubmitAfter).catch(() => {
            this.$message.newFun('提交出错', 'error', 3)
            this.modal.load = false
          })
      }
    },
    async Submit (url, msg, data) {
      if (!url) {
        return await { isSuccess: false, msg }
      }
      this.modal.load = true
      if (this.modal.customSubmit instanceof Function) {
        return await this.modal.customSubmit(url, this.submitTransform(data))
      } else {
        return await url(this.submitTransform(data))
      }
    },
    SubmitAfter ({ isSuccess, msg }) {
      if (isSuccess) {
        this.$message.newFun(msg, 'success', 3)
        this.$emit('ok')
        this.close()
      } else {
        this.$message.newFun(msg, 'error', 3)
      }
      this.modal.load = false
    },
    // 关闭模态框
    close () {
      if (Object.hasOwnProperty.call(this.$refs, 'form')) {
        this.$refs.form.resetFields()
      }
      this.modal.show = false
      this.modal.load = false
      this.$emit('showChange', false)
      this.$emit('close')
    }
  }
}
